/*
 * FileUpload.java
 *
 * Created on April 10, 2007, 6:19 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package finalintex2web;
import java.util.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.FileItem;

/**
 *
 * @author craig
 */
public class FileUpload implements Action {
    
    /** Creates a new instance of FileUpload */
    public FileUpload() {
    }
    
    /**
   *  Responds to an action call from the Controller.java file.
   *  This method should perform any work required, then return
   *  a new JSP page to call.
   *
   *  @param request  The HttpServletRequest object that represents
   *                  information sent from the browser.  The getParameter()
   *                  method is particularly useful.
   *
   *  @param response The HttpServletResponse object that represents
   *                  information sent back to the browser.  Normally
   *                  you won't use this object as the JSP page that
   *                  gets forwarded to sends information back to the 
   *                  browser.
   *
   *  @returns        A string giving the path of the JSP file to call
   *                  after this action is performed.  If you need this
   *                  to be dynamic, use hidden form field to send
   *                  a parameter giving the page to go to after the
   *                  action.  
   *
   *                  If the path starts with "/", the path is 
   *                  absolute to the application context.  If the 
   *                  path doesn't start with "/", it is relative
   *                  to the current page (dangerous!).
   */
  public String process(HttpServletRequest request, HttpServletResponse response)
    throws Exception{
      
 
      
      //System.out.println("Myname is (regular request object):" + request.getParameter("myname"));
        
        // parse the request using the Jakarta file upload library
        // convert the requestParams into a map.  this assumes that each form field on
        // the page has a unique name.  if two fields have the same name, the second
        // one will blast the first one
        ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());
        List paramList = servletFileUpload.parseRequest(request);
        Map params = new TreeMap();
        for (int i = 0; i < paramList.size(); i++) {
            FileItem item = (FileItem)paramList.get(i);
            params.put(item.getFieldName(), item);
        }//for
            
        // get a regular parameter from the request
       // String myname = ((FileItem)params.get("myname")).getString();
        //System.out.println(myname);
        
        // get a file from the request
        
        FileItem fileinfo = (FileItem)params.get("file");
        FileBO file = FileDAO.getInstance().create();
        file.setName(fileinfo.getName());
        file.setFile(fileinfo.get());
        //file.setBackup();
        //String extension = file.getName()
        //file.setExtension("");
        FileDAO.getInstance().save(file);
    
                
        // the fileitem also has the bytes of the file.  you could easily call
        // setBinaryStream in your PreparedStatement since fileitem.getInputStream
        // gives you an input stream, which is what setBinaryStream wants.
             
     
      return "backup.jsp";
  }
  
}


